package com.advertising.permissions.tree;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.advertising.permissions.model.Resources;
import com.tgyt.tree.Node;
import com.tgyt.tree.TreeDirector;
import com.tgyt.tree.TreeModel;
import com.tgyt.tree.UserDataUncoder;
import com.tgyt.tree.easyui.EasyuiTreeBuilder;
import com.tgyt.tree.easyui.TgytTreeBuilder;
import com.tgyt.tree.support.AbstractWebTreeBuilder;
import com.tgyt.tree.support.AbstractWebTreeModelCreator;
import com.tgyt.tree.support.DefaultNodeComparator;
import com.tgyt.tree.support.DefaultTreeDirector;
import com.tgyt.tree.support.DefaultTreeModel;
import com.tgyt.tree.support.ReverseComparator;
import com.tgyt.tree.support.WebTreeNode;

public class Utils {
	public static String showeasyuiTree(UserDataUncoder orgUncoder,AbstractWebTreeModelCreator treeModelCreator,List list,HttpServletRequest qRequest){
		
		treeModelCreator.init(qRequest);
		TreeModel tempModel = treeModelCreator.create(list, orgUncoder);
		WebTreeNode virtualRootNode = new WebTreeNode("根节点", "Virtual");
		java.util.Iterator rootNodes = tempModel.getRootNodes();
		while (rootNodes.hasNext()) {
			Node rootNode = (Node) rootNodes.next();
			rootNode.setParent(virtualRootNode);
		}
		DefaultTreeModel treeModel = new DefaultTreeModel();
		treeModel.addRootNode(virtualRootNode);
		TreeDirector director = new DefaultTreeDirector();// 构造树导向器
		director.setComparator(new ReverseComparator(new DefaultNodeComparator()));
		AbstractWebTreeBuilder treeBuilder = new EasyuiTreeBuilder();// 构造树Builder
		treeBuilder.init(qRequest);
		director.build(treeModel, treeBuilder);// 执行构造
		return treeBuilder.getTreeScript();
	}
	
	public static String getTree(UserDataUncoder orgUncoder,AbstractWebTreeModelCreator treeModelCreator,List list,HttpServletRequest qRequest){
		treeModelCreator.init(qRequest);
		TreeModel tempModel = treeModelCreator.create(list, orgUncoder);
		
		WebTreeNode virtualRootNode = new WebTreeNode("根节点", "Virtual");
		java.util.Iterator rootNodes = tempModel.getRootNodes();
		while (rootNodes.hasNext()) {
			Node rootNode = (Node) rootNodes.next();
			rootNode.setParent(virtualRootNode);
		}
		DefaultTreeModel treeModel = new DefaultTreeModel();
		treeModel.addRootNode(virtualRootNode);
		TreeDirector director = new DefaultTreeDirector();// 构造树导向器
		director.setComparator(new ReverseComparator(new DefaultNodeComparator()));
		AbstractWebTreeBuilder treeBuilder = new TgytTreeBuilder();// 构造树Builder
		treeBuilder.init(qRequest);
		director.build(treeModel, treeBuilder);// 执行构造
		return treeBuilder.getTreeScript();
	}
	
	public static String  getLeft(List<Resources> list){
		StringBuffer  srtingBuffer = new StringBuffer();
		List<Resources> tempList = new ArrayList<Resources>(); 
		for (Resources resources : list) {
			  if(resources.getParent()==null){
				 // srtingBuffer.append("<h2 style='background:url("+object.getIcon()+")'>"+object.getName()+"</h2>");
				  tempList.add(resources);
				  
			  }
		}
		for (Resources resources1 : tempList) {
			srtingBuffer.append("<h2 style='background:url(images/main_34.gif)'>"+resources1.getName()+"</h2>");
			srtingBuffer.append("<div class='content' style='border-color:#FFFFFF';''>");
			for (Resources resources2 : list) {
				 if (resources2.getParent()!=null&&resources1.getId()==resources2.getParent().getId()) {
						srtingBuffer.append("<table width='100%' border='0' cellspacing='0' cellpadding='0'>");
						srtingBuffer.append("<tr>");
							srtingBuffer.append("<td width='33' height='28'><img src='images/main_52.gif'");
								srtingBuffer.append("width='28' height='28'>");
							srtingBuffer.append("</td>");
							srtingBuffer.append("<td width='99'><table width='100%' border='0' cellspacing='0'");
									srtingBuffer.append("cellpadding='0'>");
									srtingBuffer.append("<tr>");
										srtingBuffer.append("<td height='23' class='STYLE4' style='cursor:hand'");
											srtingBuffer.append(" onMouseOver=\"this.style.backgroundImage='url(images/tab_bg.gif)';this.style.borderStyle='solid';this.style.borderWidth='1';borderColor='#adb9c2'; \"");
											srtingBuffer.append(" onmouseout=\"this.style.backgroundImage='url()';this.style.borderStyle='none'\"");
											srtingBuffer.append(" onClick=\"window.open( '"+resources2.getLink()+"', 'mainiFrame');\">"+resources2.getName()+"</td>");
									srtingBuffer.append("</tr>");
								srtingBuffer.append("</table>");
							srtingBuffer.append("</td>");
						srtingBuffer.append("</tr>");
					srtingBuffer.append("</table>");
				}
			}
			
			srtingBuffer.append("</div>");
		}
		
		return srtingBuffer.toString();
	}
	
	
}
